StateMachineEntityManager<TContext, TEntity, TId, TStateType>

A Manager inheriting from that contains reusable logic for updating a TEntity’s current State.

Syntax

public abstract class StateMachineEntityManager<TContext, TEntity, TId, TStateType> : IdentifiableEntityManager<TContext, TEntity, TId> where TContext : DbContext where TEntity : class, IIdentifiable<TId>, IHasState<TStateType> where TId : struct where TStateType : class, IDbStateEnum

Inheritance

Constructors

StateMachineEntityManager

protected StateMachineEntityManager(TContext dataContext, IMessagePublisher messagePublisher)
Initializes a new instance of the StateMachineEntityManager class.

Parameters

  • dataContext TContext: The instance to use for the database connection. Should be injected by the DI container.
  • messagePublisher IMessagePublisher: The SimpleMessageBus instance to use to publish Messages to a Queue. Should be injected by the DI container.

Properties

StateTypes

public List<TStateType> StateTypes { get; }
Gets the collection of active state types available for entities managed by this manager. This collection is populated during initialization from the database.

Returns

List<TStateType>

Methods

Initialize

public virtual void Initialize()
Initializes the StateTypes collection by loading active state types from the database. This method is called automatically by state update methods if the collection is empty.

SetCancelledAsync

public virtual Task<bool> SetCancelledAsync(TEntity entity)
Sets the entity’s state to “Cancelled” (sort order 98).

Parameters

  • entity TEntity: The entity to update.

Returns

Task<bool> True if the state was successfully updated; otherwise, false.

SetCompletedAsync

public virtual Task<bool> SetCompletedAsync(TEntity entity)
Sets the entity’s state to “Completed” (sort order 100).

Parameters

  • entity TEntity: The entity to update.

Returns

Task<bool> True if the state was successfully updated; otherwise, false.

SetCreatedAsync

public Task<bool> SetCreatedAsync(TEntity entity)
Sets the entity’s state to “Created” (sort order 0).

Parameters

  • entity TEntity: The entity to update.

Returns

Task<bool> True if the state was successfully updated; otherwise, false.

SetFailedAsync

public virtual Task<bool> SetFailedAsync(TEntity entity, string errorMessage = "", string errorDetail = "")
Sets the entity’s state to “Failed” (sort order 99).

Parameters

  • entity TEntity: The entity to update.
  • errorMessage string: Optional error message (currently not used in implementation).
  • errorDetail string: Optional error detail (currently not used in implementation).

Returns

Task<bool> True if the state was successfully updated; otherwise, false.

UpdateStateAsync

public Task<bool> UpdateStateAsync(TEntity entity, int sortOrder)
Updates the entity’s state to the state type with the specified sort order. Logs the state transition for tracking purposes.

Parameters

  • entity TEntity: The entity to update.
  • sortOrder int: The sort order of the target state type.

Returns

Task<bool> True if the state was successfully updated; otherwise, false.

Exceptions

  • [Exception](https://learn.microsoft.com/dotnet/api/system.exception): Thrown when no state type is found with the specified sort order.